package com.qf.main.login.controller;

import com.github.pagehelper.PageInfo;
import com.qf.common.base.result.ResponseResult;
import com.qf.common.base.result.ResultStatus;
import com.qf.main.login.common.qo.SysUserQo;
import com.qf.main.login.common.qo.UsernamePasswordQo;
import com.qf.main.login.common.vo.SysUserVo;
import com.qf.main.login.config.LoginType;
import com.qf.main.login.entity.SysUser;
import com.qf.main.login.realm.UserToken;
import com.qf.main.login.service.SysUserRoleService;
import com.qf.main.login.service.SysUserService;
import com.qf.main.login.service.SmsService;
import com.qf.main.login.utils.JwtUtils;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.swing.*;

/**
 * @author chengqiang
 * @Create 2024-01-18-21:20
 * @Description:
 */
@RestController
@RequestMapping("/user")
@Api(tags = "登录权限认证")
@CrossOrigin
public class SysUserController {
    @Resource
    JwtUtils jwtUtils;

    @Resource(name = "SysUserService")
    private SysUserService sysUserService;

    @Resource(name ="SysUserRoleService" )
    private SysUserRoleService sysUserRoleService;
    @Resource
    private SmsService smsService;


    @PostMapping("/login")
    @ResponseBody
    @ApiOperation("密码登录")
    // @ApiImplicitParams({@ApiImplicitParam(name = "username", value = "账号"),@ApiImplicitParam(name = "password", value = "密码")})
    @ApiResponses({@ApiResponse(code = 200, message = "成功", response = String.class), @ApiResponse(code = 400, message = "系统错误")})
    public ResponseResult<SysUserVo> doLogin(@RequestBody UsernamePasswordQo usernamePasswordQo) {
        String username= usernamePasswordQo.getUsername();
        String password= usernamePasswordQo.getPassword();
        return login(username,password,LoginType.USER_PASSWORD);
    }


    @GetMapping("/no/login")
    @ResponseBody
    @ApiOperation("没有登录")
    @ApiResponses({@ApiResponse(code = 200, message = "成功", response = String.class), @ApiResponse(code = 400, message = "系统错误")})
    public ResponseResult<String> noLogin(){
        return ResponseResult.error(ResultStatus.USER_LOGGED_NO);
    }

    @GetMapping("/register/valid")
    @ResponseBody
    @ApiOperation("手机验证")
    public ResponseResult<Integer> registerValid( @RequestParam(value = "phone" ,required = false) String phone,
                                                  @RequestParam(value ="code" , required = false)String code){
        return ResponseResult.success(smsService.registerValid(phone,code));
    }
    @PostMapping("/register")
    @ResponseBody
    @ApiOperation("注册")
    @ApiResponses({@ApiResponse(code = 200, message = "成功", response = String.class),
            @ApiResponse(code = 400, message = "系统错误")})
    public ResponseResult<Integer> doRegister(@RequestBody SysUserQo sysUserQo){
        if(sysUserQo.getRoleId()==null) {
            sysUserQo.setRoleId(2);
        }
       return ResponseResult.success(sysUserService.saveSysUser(sysUserQo));
    }

    @PutMapping("/edit/password")
    @ResponseBody
    @ApiOperation("修改用户")
    // 修改信息必须传入用户名和密码，
    @ApiResponses({@ApiResponse(code = 200, message = "成功", response = String.class), @ApiResponse(code = 400, message = "系统错误")})
    public ResponseResult<Integer> editSysUserPassword (@RequestBody SysUserQo sysUserQo,@RequestParam("oldPassword") String oldPassword,@RequestParam("ConfirmPassword")String ConfirmPassword){


        Integer integer = sysUserService.modifySysUserPassword(sysUserQo,oldPassword,ConfirmPassword);

        return ResponseResult.success(integer);
    }
    @PutMapping("/edit")
    @ResponseBody
    @ApiOperation("修改用户")
    // 修改信息必须传入用户名和密码，
    @ApiResponses({@ApiResponse(code = 200, message = "成功", response = String.class), @ApiResponse(code = 400, message = "系统错误")})
    public ResponseResult<Integer> editSysUser (@RequestBody SysUserQo sysUserQo){


        Integer integer = sysUserService.modifySysUser(sysUserQo);

        return ResponseResult.success(integer);
    }

    @GetMapping("/list")
    @ResponseBody
    @ApiOperation("查询用户信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "页码"),@ApiImplicitParam(name = "pageSize", value = "条数")})
    public ResponseResult<PageInfo<SysUserVo>> listSysUser(@RequestParam(value = "pageNum" ,required = false) Integer pageNum, @RequestParam(value = "pageSize" ,required = false) Integer pageSize,
                                                           @RequestParam(value = "username" ,required = false) String username,@RequestParam(value ="phone" , required = false)String phone){
        if(pageNum==null||pageNum==0){
            pageNum=1;
        }
        if(pageSize==null||pageSize==0){
            pageSize=10;
        }
        return ResponseResult.success(sysUserService.listUser(username,phone,pageNum,pageSize));
    }


    @GetMapping("/test")
    @ResponseBody
    @RequiresRoles(value = {"ROLE_ADMIN"})
    @ApiOperation("测试")
    @ApiResponses({@ApiResponse(code = 200, message = "成功", response = String.class), @ApiResponse(code = 400, message = "系统错误")})
    public String test(){
        return "测试";
    }


    @GetMapping("/no/unauthorized")
    @ResponseBody
    @ApiOperation("权限不足")
    @ApiResponses({@ApiResponse(code = 200, message = "成功", response = String.class), @ApiResponse(code = 400, message = "系统错误")})
    public ResponseResult<String> noPermission(){
        return ResponseResult.error(ResultStatus.USER_PERMISSION_NO);
    }



    @ApiOperation("获取验证码")
    @ApiImplicitParam(name = "phone", value = "手机号")
    @PostMapping("/verify/code")
    public ResponseResult<String> verificationCode(String phone) {
        return ResponseResult.success(smsService.sendSms(phone));
    }

    @ApiOperation("验证码登录")
    @ApiImplicitParam(name = "phone", value = "手机号")
    @PostMapping("/phone/login")
    public ResponseResult<SysUserVo> loginPhone(String phone,String code) {

        return login(phone,code,LoginType.USER_PHONE);
    }
    @DeleteMapping("/erase")
    public ResponseResult<Integer> eraseUser(@RequestParam(value = "id" )Integer id){

        return ResponseResult.success(sysUserService.removeUser(id));
    }
    @DeleteMapping("/del")
    public ResponseResult<Integer> delUser(@RequestParam(value = "id" )Integer id){
        SysUserQo sysUserQo = new SysUserQo();
        sysUserQo.setId(id);
        sysUserQo.setDelStatus(1);
        return ResponseResult.success(sysUserService.modifySysUser(sysUserQo));
    }
    @DeleteMapping("/recover")
    public ResponseResult<Integer> recoverUser(@RequestParam(value = "id" )Integer id){
        return ResponseResult.success(sysUserService.recoverUser(id));
    }

    // 登录方式
    public ResponseResult<SysUserVo> login(String username, String password, LoginType loginType) {
        // 可以变成一条语句
        UserToken userToken = new UserToken(loginType, username, password);
        if(LoginType.USER_PASSWORD.equals(loginType)){
            return ResponseResult.success(sysUserService.login(userToken));
        }else if (LoginType.USER_PHONE.equals(loginType)){
            return ResponseResult.success(smsService.loginPhone(userToken));
        }
       return ResponseResult.error();
    }

    // 设置头像
    @PostMapping("/upImages")
    public ResponseResult<String> upImages(@RequestParam("file")MultipartFile file) {

        return ResponseResult.success(sysUserService.uploadImage(file,"user"));
    }

}
